<?php

namespace app\api\controller\v2;

use app\api\logic\Category;
use app\api\logic\TaskLogic;
use app\common\controller\Api;
use app\common\model\Favorite;
use app\common\model\zbtask\Order;
use think\Db;

/**
 * 首页接口
 */
class Task extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 获取所有分类
     */
    public function getCate() {
        $cate = Category::instance()->getAll(null);  //服务行业
        $this->success('cate', $cate);
    }
    /**
     * 获取所有分类
     */
    public function getTask() {
        $zbcategory_id = input('zbcategory_id/d', 0);
        if (!$zbcategory_id) {
            $this->error('参数传递错误');
        }

        $list = TaskLogic::instance()->where(['zbcategory_id' => $zbcategory_id])->taskPages()->toArray();
        //展示时，处理html特殊标签
       $b_str = 'source="" sans="" pro",="" "helvetica="" neue",="" helvetica,="" arial,="" sans-serif;white-space:normal;background-color:#ffffff;';
       $b_str2 = 'line-height:14.82px;"=""';
        foreach($list['data'] as $key => $value){
            $count = $value['content'];
            $count =str_replace($b_str, '', $count);
            $count =str_replace($b_str2, '', $count);
            $list['data'][$key]['content'] = $count;
        }
        $this->success('任务', $list);
    }

    /**
     * 我的任务
     */
    public function mytask() {
        // 0为申请中，1 处理中 2  已完成
        $type = input('type/d', 0);
        $where['user_id'] = UID;
        $where['type'] = $type;
        $where['status'] = 1;
        $ids = Order::where($where)->column('task_id');
        $list = TaskLogic::instance()->where(['id'=> ['in', $ids]])->taskPages();
        $this->success('任务', $list);
    }

    /*获取接单页面 */
    public function replyTask() {
        $task_id = input('task_id/d', 0);
        $task= TaskLogic::instance()->getSingle($task_id)->toArray();
        $b_str = 'source="" sans="" pro",="" "helvetica="" neue",="" helvetica,="" arial,="" sans-serif;white-space:normal;background-color:#ffffff;';
        $b_str2 = 'line-height:14.82px;"=""';
        $count = $task['content'];
        $count =str_replace($b_str, '', $count);
        $count =str_replace($b_str2, '', $count);
        $task['content'] = $count;
        $list = TaskLogic::instance()->where(['id'=> ['neq',$task_id]])->getTask(3);

        $data['ortherTask'] = $list;
        $data['task'] = $task;
        //是否收藏
        $favorite = Db::name('favorite')->where(['task_id'=>$task_id,'user_id'=>UID])->count();
        if ($favorite > 0) {
            $data['favorite'] = 1;
        } else {
            $data['favorite'] = 0;
        }
        $is_receive = TaskLogic::instance()->isReceive(UID, $task_id);
        $data['is_receive'] = $is_receive;
        $this->success('接单页面', $data);
    }

    /**
     * 立即接单
     */
    public function replay() {
        $this->success('线上接单功能，还未开启');
        $task_id = input('task_id/d', 0);
        TaskLogic::instance()->createOrder(UID, $task_id);

        $this->success('提交申请成功');
    }

    /**收藏*/
    public function collect() {
        $task_id = input('task_id/d', 0);
        if (!$task_id) {
            $this->error('参数错误');
        }
        if (Favorite::get(['task_id' => $task_id, 'user_id'=>UID])) {
            $this->error('已收藏过该任务');
        }

        $favorite = new Favorite();
        $favorite->task_id = $task_id;
        $favorite->user_id = UID;
        $favorite->isUpdate(false)->save();
        $this->success('收藏成功');
    }
}
